﻿     @model InVision_Ticket.ViewModels.TicketViewModel
@using InVision_Ticket.Utilities
@using System.Linq
@{
    ViewBag.Title = "Edit Ticket: " + Model.TicketID;
}
<script src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")" type="text/javascript"></script>   
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script> 

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src= "@Url.Content("~/Scripts/MarkdownDeepLib.min.js")" type="text/javascript"></script>
<link href="../../Content/mdd_styles.css" rel="stylesheet" type="text/css" />
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Ticket @Html.DisplayFor(model => model.TicketID)</legend>
        @Html.HiddenFor(model => model.TicketID)

        <div class="editor-label">
            Customer
        </div>
        <div class="customerInfoWrapper">
            @{
            if (Model.BusinessCustomer)
            { 
                        <div class="editor-label">
                            @Html.LabelFor(model => model.BusinessName)
                        </div>
                        <div class="editor-field">
                            @Html.EditorFor(model => model.BusinessName)
                            @Html.ValidationMessageFor(model => model.BusinessName)
                        </div>
    
            }    
            
            }


       
            <div class="editor-field bold">
                @Html.DisplayFor(model => model.CustomerContactName)
                @Html.ValidationMessageFor(model => model.CustomerContactName)
            </div>
            <div class="editor-field bold">
                @Html.DisplayFor(model => model.Phone)
                @Html.ValidationMessageFor(model => model.Phone)
            </div>
            @Html.ActionLink("Customer Information", "Details", "Customer", new { id = Model.CustomerContactID }, new { target = "_blank" })
        </div>

        <div class="system-info editor-field">
            <div class="system-info-toggle-link">
                <a id="system-info-toggle-link" href="javascript:void(0)" onclick="toggleSystemDiv();">+ System Info</a>
            </div> 
            <div id="system-info-toggle" class="system-info-toggle">

                <div class="editor-label">
                    @Html.LabelFor(model => model.System.SystemID)
                </div>
                <div class="editor-field">
                    @Html.DisplayFor(model => model.System.SystemID)
                </div>
                <div class="editor-field">
                    @Html.DisplayFor(model => model.System.Desciption)
                </div>
            </div>
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Summary)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Summary)
            @Html.ValidationMessageFor(model => model.Summary)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Details)
        </div>
            
        <div class="editor-field">
            <div class="mdd_preview" id="details_preview">@Model.Details</div>
            <a id="mdd_link" href="javascript:void(0)" onclick="toggleDetailsEdit();">+ Edit Details</a>
            <div class="mdd_toolbar" id="details_toolbar"></div>
            @Html.TextAreaFor(model => model.DetailsMarkDown, new { @class = "mdd_editor", @id = "details_textarea" })
		    <div class="mdd_resizer" id="details_resizer"></div>
        </div>
        <div class="createdWrapper ticketEditWrapper">
            <div class="editor-label">
                @Html.LabelFor(model => model.CreatedDateTime)

            </div>
            <div class="editor-field">
                @Html.DisplayFor(model => model.CreatedDateTime)
                <br />
                @Html.Raw(TimeAgo.getStringTime(Model.CreatedDateTime))
                By:
                @{
                    if (Model.CreatedByCustomer)
                    {
                            @Html.Raw("<div class=\"editor-field\"> The Customer.</div>");


                    }
                    else
                    { 
                                @Html.DisplayFor(model => model.CreatedByLogin)
                    }
                }
            </div>
        </div>
        <div class="lastModifiedWrapper ticketEditWrapper">
            @if (Model.LastModifiedDateTime.HasValue)
            { 
                <div class="editor-label">
                    @Html.LabelFor(model => model.LastModifiedDateTime)
                </div>
                <div class="editor-field">
                    @Html.DisplayFor(model => model.LastModifiedDateTime)
                <br />
                    @Html.Raw(TimeAgo.getStringTime(Model.LastModifiedDateTime.Value)) By: @Html.DisplayFor(model => model.LastModifiedBy)
                </div>

            }
        </div>

        <br />
@*        @if (Model.ResolvedDateTime.HasValue)
        {
            <div class="editor-label">
                @Html.LabelFor(model => model.ResolvedDateTime)
            </div>
            <div class="editor-field">
                @Html.DisplayFor(model => model.ResolvedDateTime)
                @Html.Raw(TimeAgo.getStringTime(Model.ResolvedDateTime.Value));
            </div>
        }
        *@

        <div class="storeLocationWrapper ticketEditWrapper">
            <div class="editor-label">
                @Html.LabelFor(model => model.Location.StoreLocation)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.Location.LocationID, new SelectList(Model.LocationList, "LocationID", "StoreLocation", Model.Location))
                @Html.ValidationMessageFor(model => model.Location)
            </div>
        </div>

        <div class="salesLoginWrapper ticketEditWrapper">
            <div class="editor-label">
                @Html.Label("SalesMan")
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.SalesLogin.LoginID, new SelectList(Model.LoginList, "LoginID", "DisplayName", Model.SalesLogin))
                @Html.ValidationMessageFor(model => model.SalesLogin)
            </div>
        </div>

        <div class="techLoginWrapper ticketEditWrapper">
            <div class="editor-label" >
                @Html.Label("Technician")
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.TechLogin.LoginID, new SelectList(Model.LoginList, "LoginID", "DisplayName", Model.TechLogin))
                @Html.ValidationMessageFor(model => model.TechLogin)
            </div>
        </div>

        <div class="statusWrapper ticketEditWrapper">
            <div class="editor-label">
                @Html.Label("Status")
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.TicketStatus.TicketStatusID, new SelectList(Model.TicketStatusList, "TicketStatusID",  "Status", Model.TicketStatus))
                @Html.ValidationMessageFor(model => model.TicketStatus)
        
            </div>
        </div>
        <div class="ticketTypeWrapper ticketEditWrapper">
            <div class="editor-label">
                @Html.LabelFor(model => model.TicketType)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.TicketTypeID, new SelectList(Model.TicketTypeList, "TicketTypeID", "TicketType1", Model.TicketType))
                @Html.ValidationMessageFor(model => model.TicketType)
            </div>
        </div>


        

        <div class="priorityWrapper ticketEditWrapper">
            <div class="editor-label">
                @Html.LabelFor(model => model.Priority)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Priority)
                @Html.ValidationMessageFor(model => model.Priority)
            </div>
        </div>




        
        <br />
        @{
            decimal charges = 0;
            foreach(var update in Model.Updates)
            {
                if (update.OtherCharges.HasValue)
                {
                    charges = charges + (update.OtherCharges.Value);
                }
                if(update.BilledMinutes.HasValue)
                {
                    charges = charges + ((update.BilledMinutes.Value/(decimal)60.0) * update.BillRate.TicketBillRate);
                }
            }
            <b>
                @Html.Raw(String.Format("Total Charges: {0:C}", charges))
            </b>
        }
        <br />
        <p>
            <input type="submit" value="Save and Close" />
        </p>
    </fieldset>
    <fieldset>
        <legend>Updates</legend>
        <input type="button" class="updateButon" onclick="Update(@Model.TicketID)" value="Update"/>
        @foreach (var update in Model.Updates.OrderByDescending(u => u.ActivityDateTime))
        { 
            <div class ="editor-label">
                <span>@Html.Raw("<span class='UpdateHeader'>" + update.Login.DisplayName + " " + update.ActivityDateTime + "</span>") &nbsp;&nbsp; @Html.ActionLink("Delete", "DeleteUpdate", "Ticket", new { id = update.UpdateID }, new { target = "_blank" }) </span><br />
                <div class="updateText">
                    @if (update.BilledMinutes.HasValue || update.ActualMinutes.HasValue || update.OtherCharges.HasValue)
                    {
                
                        <span class="UpdateInformation">Billed Minutes:@update.BilledMinutes Actual Minutes:@update.ActualMinutes BillRate: @((update.BillRate == null) ? "$0" : update.BillRate.TicketBillRate.ToString("C")) </span> <br />
                        <span class="UpdateInformation">Other Charges: @((update.OtherCharges == null) ? "$0" : update.OtherCharges.Value.ToString("C"))  
                            @((update.OtherCharges.HasValue) ? ("For:" + update.OtherChargesDescription) : "")</span>
                        <p></p>
                    }
                
                    @Html.Raw(update.Comment)
                </div>
            </div>       
        }
        <input type="button" class="updateButon" onclick="Update(@Model.TicketID)" value="Update"/>
    </fieldset>
    <fieldset>
        <legend>Attachments</legend>
        <input type="button" class="uploadButton" onclick="Upload(@Model.TicketID)" value="Upload"/>
        @foreach (var upload in Model.Uploads)
        { 
            <div>
                <span>@upload.Description</span>
                <span>@Html.ActionLink("Delete", "DeleteUpload", "Ticket", new { id = upload.UploadID }, new { target = "_blank"})  @Html.ActionLink(upload.FileName, "DownloadUpload", "Ticket", new { id = upload.UploadID }, new { target = "_blank" })</span>
            </div>
        
        }
    </fieldset>
    
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>



<script type="text/javascript">
    function Update(id) {
        window.open('/Ticket/UpdateTicket/' + id,
        '1365918452735',
        'width=700,height=500,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');
        return false;
    }
    function Upload(id) {
        window.open('/Ticket/Upload/' + id,
        '1365918452736',
        'width=700,height=500,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');
        return false;

    }
    function toggleSystemDiv()
    {
        var div = document.getElementById("system-info-toggle");
        var link = document.getElementById("system-info-toggle-link");
        if (div.style.display == "block") {
            div.style.display = "none";
            link.innerHTML = "+ System Info";
        }
        else
        {
            div.style.display = "block";
            link.innerHTML = "- System Info";
        }

    }
    function toggleDetailsEdit() {
        var text = document.getElementById("details_textarea");
        var resize = document.getElementById("details_resizer");
        var toolbar = document.getElementById("details_toolbar");
        var link = document.getElementById("mdd_link");

        if (text.style.display == "block") {
            text.style.display = "none";
            resize.style.display = "none";
            toolbar.style.display = "none";

            link.innerHTML = "+ Edit Details";
        }
        else {
            text.style.display = "block";
            resize.style.display = "block";
            toolbar.style.display = "block";
            link.innerHTML = "- Edit Details";
        }

    }
</script>


<script type="text/javascript">
    $("textarea.mdd_editor").MarkdownDeep({
        help_location: "~/Content/mdd_help.html"

    });
</script>